package com.android.music.prolyric;

import java.util.ArrayList;
import java.util.HashMap;


import com.android.music.prolyric.songmanager.FolderMusic;
import com.android.music.prolyric.songmanager.ListFolderMusic;
import com.android.music.prolyric.songmanager.Song;
import com.android.music.prolyric.songmanager.SongsManager;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class LibraryFolder extends Activity {
	private static final int STATE_SELECT_FOLDER = 0 ;
	private static final int STATE_SELECT_SONG	= 1;
	private int state = 0;
	private static final ArrayList<FolderMusic> folders  = ListFolderMusic.getFolderMusic();
	ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
	private ArrayList<HashMap<String, String>> songs = new ArrayList<HashMap<String,String>>();
	private MyAdapter myAdapter;
	
	private ListView lvFolder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.folder_list);
		//Toast.makeText(getBaseContext(), "Folder Library is created", Toast.LENGTH_LONG).show();
		lvFolder = (ListView)findViewById(R.id.lvFolder);
		 
		 state = 0;
	      
	     
	      
	       setFirstAdapter();
	       //lvFolder.setAdapter(null);
	      
	      lvFolder.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(state == STATE_SELECT_FOLDER)
				{
					songs = folders.get(arg2).getListMusicFile();
					SimpleAdapter adapter = new SimpleAdapter(LibraryFolder.this, songs, R.layout.song_list_row, new String[]{"title","artist","duration"}, new int[]{R.id.tvsongtitle,R.id.tvsongartist,R.id.tvsongduration});
					lvFolder.setAdapter(adapter);
					state = STATE_SELECT_SONG; 
				}
				else if(state == STATE_SELECT_SONG)
				{
					GlobalState globle = (GlobalState) getApplication();
					globle.setGlobalSongs(songs);
					globle.setSongIndex(arg2);
					if(getParent()==null)
						setResult(Activity.RESULT_OK);
					else 
						getParent().setResult(Activity.RESULT_OK);
					finish();
				}
				
					
				
			}
		});
	}
	
	public void setFirstAdapter()
	{
		
		myAdapter = new MyAdapter(this, folders);
	       lvFolder.setAdapter(myAdapter);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		setFirstAdapter();
		state = STATE_SELECT_FOLDER;
	}
	
	
	
}


class MyAdapter extends ArrayAdapter<FolderMusic>
{

	private final ArrayList<FolderMusic> list;
	  private final Activity context;

	public MyAdapter(Activity  context, ArrayList<FolderMusic> list) {
		super(context, R.layout.folder_list_row,list);
		this.context = context;
		this.list = list;
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
	    if (convertView == null) {
	      LayoutInflater inflator = context.getLayoutInflater();
	      view = inflator.inflate(R.layout.folder_list_row, null);
	      final ViewHolder viewHolder = new ViewHolder();
	      viewHolder.musicImg = (ImageView) view.findViewById(R.id.list_image);
	      viewHolder.tv_folder = (TextView) view.findViewById(R.id.folderName);
	      viewHolder.tv_countSong = (TextView)view.findViewById(R.id.tvFolderCountSong);
	      view.setTag(viewHolder);
	     
	    } else {
	      view = convertView;
	     
	    }
	    ViewHolder holder = (ViewHolder) view.getTag();
	    holder.tv_folder.setText(list.get(position).getPath());
	    holder.tv_countSong.setText(""+list.get(position).getListMusicFile().size());
	   
	    return view;
	  
	}
	
	
	 static class ViewHolder {
		  protected ImageView musicImg;
		  protected TextView tv_folder;
		  protected TextView tv_countSong;
		   
		    
		  }

}
  
